package br.com.fafica.sisape.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import br.com.fafica.sisape.control.ControllerPrincipal;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelaLogin extends JFrame {

	private JPanel contentPane;
	private JTextField txtUsuario;
	private JPasswordField passwordSenha;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaLogin frame = new TelaLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TelaLogin() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 900, 550);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblUsurio = new JLabel("Usu\u00E1rio:");
		lblUsurio.setForeground(new Color(0, 0, 128));
		lblUsurio.setHorizontalAlignment(SwingConstants.RIGHT);
		lblUsurio.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		lblUsurio.setBounds(288, 315, 60, 19);
		contentPane.add(lblUsurio);
		
		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setForeground(new Color(0, 0, 139));
		lblSenha.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSenha.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		lblSenha.setBounds(288, 348, 60, 19);
		contentPane.add(lblSenha);
		
		txtUsuario = new JTextField();
		txtUsuario.setForeground(new Color(0, 0, 128));
		txtUsuario.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		txtUsuario.setBounds(358, 312, 170, 25);
		contentPane.add(txtUsuario);
		txtUsuario.setColumns(10);
		
		passwordSenha = new JPasswordField();
		passwordSenha.setForeground(new Color(0, 0, 128));
		passwordSenha.setBounds(358, 347, 170, 25);
		contentPane.add(passwordSenha);
		
		JButton btnEntrar = new JButton("Entrar");
		btnEntrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String usuario = txtUsuario.getText();
				String senha = passwordSenha.getText();
				
				ControllerPrincipal control = new ControllerPrincipal();
				boolean login = control.validarLogin(usuario, senha);
				
				if(login == true){
					TelaPrincipal telaPrincipal = new TelaPrincipal();
					telaPrincipal.setLocationRelativeTo(null);
					telaPrincipal.setVisible(true);
					dispose();
				}else{
					TelaMensagemLogin telaLoginNaoEfetuado = new TelaMensagemLogin();
					telaLoginNaoEfetuado.setLocationRelativeTo(null);
					telaLoginNaoEfetuado.setVisible(true);
				}
			}
		});
		btnEntrar.setForeground(new Color(0, 0, 128));
		btnEntrar.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		btnEntrar.setBounds(538, 331, 89, 23);
		contentPane.add(btnEntrar);
		
		JLabel lblAindaNo = new JLabel("Ainda n\u00E3o \u00E9 cadastrado?");
		lblAindaNo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAindaNo.setForeground(new Color(0, 0, 128));
		lblAindaNo.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		lblAindaNo.setBounds(303, 403, 176, 19);
		contentPane.add(lblAindaNo);
		
		JButton btnCadastrese = new JButton("Cadastre-se");
		btnCadastrese.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TelaCadastroUsuario telaCadastraUsuario = new TelaCadastroUsuario();
				telaCadastraUsuario.setLocationRelativeTo(null);
				telaCadastraUsuario.setVisible(true);
			}
		});
		btnCadastrese.setForeground(new Color(0, 0, 128));
		btnCadastrese.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		btnCadastrese.setBounds(489, 403, 112, 23);
		contentPane.add(btnCadastrese);
		
		JLabel label = new JLabel("");
		label.setForeground(new Color(0, 0, 128));
		label.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		label.setIcon(new ImageIcon("arquivos\\ImagemFundo.png"));
		label.setBounds(0, 0, 894, 521);
		contentPane.add(label);
	}
}
